package net.huashitong.Indicators.web;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.huashitong.Indicators.databean.CompletionClassificationDatabean;
import net.huashitong.Indicators.databean.EarlyWarningDatabean;
import net.huashitong.Indicators.entity.CompletionClassification;
import net.huashitong.Indicators.entity.EarlyWarning;
import net.huashitong.Indicators.entity.TertiaryIndustry;
import net.huashitong.Indicators.filter.EarlyWarnFilter;
import net.huashitong.Indicators.filter.IndicatorBreakdownFilter;
import net.huashitong.Indicators.service.CompletionClassificationService;
import net.huashitong.Indicators.service.ConstructionIndustryService;
import net.huashitong.Indicators.service.EarlyWarningService;
import net.huashitong.Indicators.service.GaugeIndustryService;
import net.huashitong.Indicators.service.IndicatorBreakdownService;
import net.huashitong.Indicators.service.SocialZeroService;
import net.huashitong.Indicators.service.TertiaryIndustryService;
import net.huashitong.Indicators.utils.Comparators;
import net.huashitong.Indicators.utils.ComparatorsUtils;
import net.huashitong.Indicators.utils.ThiTitleUtils;
import net.huashitong.user.entity.Collection;
import net.huashitong.user.service.CollectionService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.ObjectUtils;

@Controller
@RequestMapping("/app/earlyWarning")
public class EarlyWarningController extends JsonBaseController {

	@Autowired
	private EarlyWarningService earlyWarningService;
	@Autowired
	private ConstructionIndustryService constructionIndustryService;
	@Autowired
	private GaugeIndustryService gaugeIndustryService;
	@Autowired
	private SocialZeroService socialZeroService;
	@Autowired
	private TertiaryIndustryService industryService;
	@Autowired
	private IndicatorBreakdownService breakdownService;
	@Autowired
	private CompletionClassificationService classificationService;
	@Autowired
	private CollectionService collectionService;
	@Autowired
	private TertiaryIndustryService tertiaryIndustryService;
	@Override
	protected String getView(String arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	/**
	 * 获取预警数据
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/queryEarlyWarning")
	public Message queryEarlyWarning(){
		List<EarlyWarning> earlyWarnings = earlyWarningService.query();
		List<EarlyWarningDatabean> databeans = new ArrayList<EarlyWarningDatabean>();
		for (EarlyWarning earlyWarning : earlyWarnings) {
			EarlyWarningDatabean databean = new EarlyWarningDatabean();
			TertiaryIndustry tertiaryIndustry=tertiaryIndustryService.queryBySysId(earlyWarning.getIndicatorId());
			databean.setTitle(earlyWarning.getTitle());
			databean.setSysId(earlyWarning.getSysId());
			databean.setDate(earlyWarning.getCreateDate());
			databean.setLeadership(tertiaryIndustry.getLeadership());
			databean.setUnitsNumber(tertiaryIndustry.getUnitsNumber());
			databeans.add(databean);
		}
		return coverMessage("200", "获取成功", databeans);
	}
	/**
	 * 读取图表数据
	 * @param filter
	 * @return
	 * @throws ParseException 
	 */
	@ResponseBody
	@RequestMapping("/getEarlyWarningPhoto")
	public Message getEarlyWarningPhoto(@RequestBody EarlyWarnFilter filter) throws ParseException{
		EarlyWarning earlyWarning = earlyWarningService.getById(filter.getSysId());
		Collection collection = collectionService.getByuserIdAndAlertId(filter);
		List<Map<String, Object>> maps = new ArrayList<Map<String,Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		IndicatorBreakdownFilter breakdownFilter = new IndicatorBreakdownFilter();
		breakdownFilter.setDate(earlyWarning.getDate());
		breakdownFilter.setCurrentMonth("201706");
		breakdownFilter.setArea(earlyWarning.getArea());
		breakdownFilter.setIndustryName(earlyWarning.getIndustryName());
		breakdownFilter.setIndustryId(earlyWarning.getIndicatorId());
		if(earlyWarning.getChartPosition().equals("5")){
			if(earlyWarning.getTitle().contains("固定资产投资乡镇")){
				maps = getGTXZ(breakdownFilter);
			}else if(earlyWarning.getTitle().contains("固定资产投资部门")){
				maps = getGTBM(breakdownFilter);
			}else if(earlyWarning.getTitle().contains("建筑业增加值企业")){
				maps = getJZYQY(breakdownFilter);
			}else if(earlyWarning.getTitle().contains("社会消费品零售总额乡镇")){
				maps = getSLXZ(breakdownFilter);
			}
		}else if(earlyWarning.getChartPosition().equals("7")){
			if(earlyWarning.getTitle().contains("固定资产投资企业")){
				maps = getGTQY(breakdownFilter);
			}
		}else if(earlyWarning.getChartPosition().equals("10")){
			if(earlyWarning.getTitle().contains("规模以上工业增加值行业")){
				maps = getGSHY(breakdownFilter);
			}
		}
		if(ObjectUtils.isNotBlank(collection)){
			map.put("collectId", collection.getSysId());
		}else {
			map.put("collectId", "");
		}
		maps.add(map);
		return coverMessage("200", "数据获取成功", maps);
	}
	private List<Map<String, Object>> getGTQY(
			IndicatorBreakdownFilter breakdownFilter) {
		breakdownFilter.setQy("企业");
		List<CompletionClassification> classificationds = classificationService.queryByDWValue(breakdownFilter);
		List<CompletionClassificationDatabean> classificationDatabeands = new ArrayList<CompletionClassificationDatabean>(); 
		for (CompletionClassification completionClassification : classificationds) {
			CompletionClassificationDatabean classification = new CompletionClassificationDatabean();
			classification.setActualCompletionValue(completionClassification.getActualCompletionValue());
			classification.setPlanneValue(completionClassification.getPlanneValue());
			Double doubleds1 = Double.parseDouble(completionClassification.getActualCompletionValue());
			Double doubleds2 = Double.parseDouble(completionClassification.getPlanneValue());
			Double doubleds3 = doubleds1/doubleds2*100;
			DecimalFormat df=new DecimalFormat(".##");
			if(doubleds3<50){
				classification.setYujz("0");
			}else {
				classification.setYujz("1");
			}
			if(doubleds3 == 0){
				classification.setCompleteAnnual(Double.toString(doubleds3));
			}else {
				String st=df.format(doubleds3);
				classification.setCompleteAnnual(st);
			}
			classification.setCategoryDetails(completionClassification.getCategoryDetails());
			classificationDatabeands.add(classification);
		}
		Collections.sort(classificationDatabeands, Comparators.getComparator()); 
		List<CompletionClassificationDatabean> classificationDatabeands2 = new ArrayList<CompletionClassificationDatabean>(); 
		List<CompletionClassificationDatabean> classificationDatabeands3 = new ArrayList<CompletionClassificationDatabean>();
		for (int i = 0; i < classificationDatabeands.size(); i++) {
			int y = classificationDatabeands.size();
			if(i == 6){
				break;
			}
			classificationDatabeands2.add(classificationDatabeands.get(i));
			classificationDatabeands3.add(classificationDatabeands.get(y-i-1));
		}
		List<Map<String, Object>> listd2 = new ArrayList<Map<String,Object>>();
		Map<String, Object> mapd3s = new HashMap<String, Object>();
		mapd3s.put("qliu", classificationDatabeands2);
		mapd3s.put("mliu", classificationDatabeands3);
		listd2.add(mapd3s);
		List<Map<String, Object>> maps = new ArrayList<Map<String,Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("type", "7");
		map.put("data", listd2);
		map.put("descOne", ThiTitleUtils.JSDW);
		map.put("descSec", ThiTitleUtils.WCBL);
		map.put("descThi", ThiTitleUtils.WWCBL);
		map.put("descFou", ThiTitleUtils.WCS);
		map.put("descFiv", ThiTitleUtils.JHS);
		map.put("title", "该指标各单位完成情况");
		map.put("unit", "万元");
		map.put("secTitle", "固定资产投资总额各单位完成情况");
		maps.add(map);
		return maps;
	}
	private List<Map<String, Object>> getSLXZ(
			IndicatorBreakdownFilter breakdownFilter) {
		breakdownFilter.setXz("乡镇");
		List<CompletionClassificationDatabean> classificationDatabean2s = socialZeroService.queryByXZValue(breakdownFilter);
		List<Map<String, Object>> maps = new ArrayList<Map<String,Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("type", "5");
		map.put("unit", "万元");
		map.put("data", classificationDatabean2s);
		map.put("title", "该指标各乡镇同比增长情况");
		map.put("secTitle", "社会消费品零售总额各乡镇运行情况");
		maps.add(map);
		return maps;
	}
	private List<Map<String, Object>> getGSHY(
			IndicatorBreakdownFilter breakdownFilter) throws ParseException {
		breakdownFilter.setHy("行业");
		List<CompletionClassificationDatabean> classificationDatabean2s = gaugeIndustryService.queryByHYValue(breakdownFilter);
		List<Map<String, Object>> maps = new ArrayList<Map<String,Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("type", "10");
		map.put("data", classificationDatabean2s);
		map.put("unit", "万元");
		map.put("title", "该指标各行业分类占比情况");
		map.put("secTitle", "规模以上工业增加值各行业完成情况");
		maps.add(map);
		return maps;
	}
	private List<Map<String, Object>> getJZYQY(
			IndicatorBreakdownFilter breakdownFilter) {
		breakdownFilter.setQy("企业");
		List<CompletionClassificationDatabean> classifications = constructionIndustryService.queryByDWValueSec(breakdownFilter);
		List<Map<String, Object>> maps = new ArrayList<Map<String,Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("type", "5");
		map.put("unit", "亿元");
		map.put("data", classifications);
		map.put("title", "该指标需关注企业运行情况");
		map.put("secTitle", "建筑业总产值前20家企业运行情况");
		maps.add(map);
		return maps;
	}
	private List<Map<String, Object>> getGTBM(
			IndicatorBreakdownFilter breakdownFilter) {
		//部门累计完成情况
		breakdownFilter.setBm("部门");
		List<Map<String, Object>> maps = new ArrayList<Map<String,Object>>();
		List<CompletionClassification> classifications = classificationService.queryByValue(breakdownFilter);
		List<CompletionClassificationDatabean> classificationDatabeans = new ArrayList<CompletionClassificationDatabean>(); 
		for (CompletionClassification completionClassification : classifications) {
			CompletionClassificationDatabean classification = new CompletionClassificationDatabean();
			if(completionClassification.getActualCompletionValue().length()>4&&completionClassification.getPlanneValue().length()>4){
				classification.setActualCompletionValue(completionClassification.getActualCompletionValue().substring(0, completionClassification.getActualCompletionValue().length()-4)+
						"."+completionClassification.getActualCompletionValue().substring(completionClassification.getActualCompletionValue().length()-4, completionClassification.getActualCompletionValue().length()-3));
				classification.setPlanneValue(completionClassification.getPlanneValue().substring(0, completionClassification.getPlanneValue().length()-4)+
						"."+completionClassification.getPlanneValue().substring(completionClassification.getPlanneValue().length()-4, completionClassification.getPlanneValue().length()-3));
			}else {
				continue;
			}
			Double doublebs1 = Double.parseDouble(completionClassification.getActualCompletionValue());
			Double doublebs2 = Double.parseDouble(completionClassification.getPlanneValue());
			Double doublebs3 = doublebs1/doublebs2*100;
			DecimalFormat df=new DecimalFormat(".##");
			if(doublebs3<50){
				classification.setYujz("0");
			}else {
				classification.setYujz("1");
			}
			String st=df.format(doublebs3);
			
			classification.setCompleteAnnual(st);
			classification.setCategoryDetails(completionClassification.getCategoryDetails());
			classificationDatabeans.add(classification);
		}
		Collections.sort(classificationDatabeans, new ComparatorsUtils());
		List<CompletionClassificationDatabean> classificationDatabeans2 = new ArrayList<CompletionClassificationDatabean>(); 
		int k = 10;
		if(classificationDatabeans.size()<10){
			k = classificationDatabeans.size();
		}
		for (int i = 0; i < k; i++) {
			classificationDatabeans2.add(classificationDatabeans.get(i));
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("type", "5");
		map.put("unit", "亿元");
		map.put("descOne", ThiTitleUtils.WCBL);
		map.put("descTwo", ThiTitleUtils.JSDW);
		map.put("descThi", ThiTitleUtils.WCS);
		map.put("descFou", ThiTitleUtils.JHS);
		map.put("data", classificationDatabeans2);
		map.put("title", "该指标各部门完成情况");
		map.put("secTitle", "固定资产投资总额前十大部门累计完成情况");
		maps.add(map);
		return maps;
	}
	private List<Map<String, Object>> getGTXZ(IndicatorBreakdownFilter breakdownFilter) {
		breakdownFilter.setXz("乡镇");
		List<CompletionClassification> classification2s = classificationService.queryByXZValue(breakdownFilter);
		List<CompletionClassificationDatabean> classificationDatabean2s = new ArrayList<CompletionClassificationDatabean>();
		List<Map<String, Object>> maps = new ArrayList<Map<String,Object>>();
		for (CompletionClassification completionClassification : classification2s) {
			CompletionClassificationDatabean classification = new CompletionClassificationDatabean();
			classification.setActualCompletionValue(completionClassification.getActualCompletionValue());
			classification.setPlanneValue(completionClassification.getPlanneValue());
			classification.setCategoryDetails(completionClassification.getCategoryDetails());
			Double doublebs1 = Double.parseDouble(completionClassification.getActualCompletionValue());
			Double doublebs2 = Double.parseDouble(completionClassification.getPlanneValue());
			Double doublebs3 = doublebs1/doublebs2*100;
			DecimalFormat df=new DecimalFormat("#.##");
			if(doublebs3<50){
				classification.setYujz("0");
			}else {
				classification.setYujz("1");
			}
			String st=df.format(doublebs3);
			if("0".equals(completionClassification.getActualCompletionValue())){
				classification.setCompleteAnnual("0");
			}else {
				classification.setCompleteAnnual(st);
			}
			classificationDatabean2s.add(classification);
		}
		Collections.sort(classificationDatabean2s, new ComparatorsUtils()); 
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("type", "5");
		map.put("unit", "万元");
		map.put("descOne", ThiTitleUtils.WCBL);
		map.put("descTwo", ThiTitleUtils.JSDW);
		map.put("descThi", ThiTitleUtils.WCS);
		map.put("descFou", ThiTitleUtils.JHS);
		map.put("data", classificationDatabean2s);
		map.put("title", "该指标各乡镇完成情况");
		map.put("secTitle", "分乡镇50万元以上固定资产投资总额完成情况");
		maps.add(map);
		return maps;
	}
	
}
